PRO BOX, X0, Y0, X1, Y1, color
  ; Call POLYFILL:
  POLYFILL, [X0, X0, X1, X1], [Y0, Y1, Y1, Y0], COL = color
END

PRO LEGENDPLOT, particles, colors1, colors2
  FOR i = 0, n_elements(particles) - 1 DO BEGIN
    BOX, 0, i, 1, i + 1, colors1[i]
    BOX, 1, i, 2, i + 1, colors2[i]
    xyouts, 2.5, i + 0.4, particles[i], charsize=0.4
  ENDFOR
END

PRO BARPLOT, data, colors1, colors2
  start = 1
  y0 = 0
  partID = 0
  FOR i = 0, 6 DO BEGIN
    x0 = start + i
    x1 = start + i + 1
    y1 = data[partID]
    BOX, x0, y0, x1, y1, colors1[i]
    x0++
    x1++
    partID++
    y1 = data[partID]
    BOX, x0, y0, x1, y1, colors2[i]
    start += 2
    partID++
  ENDFOR
END

PRO PLOT_VALUES, data
  values = uint(data)
  strValues = string(values)
  xyouts, [-0.05,0.95,2.95,3.95,5.95,6.95,8.95,9.95,11.95,12.95,14.95,15.95,17.95,18.95], values + 2, strValues, charsize=0.5
END

PRO PLOT_XLABELS, data
  xyouts, [0.4,4.4,6.4,10.3,12.4,16.4,18.5], [-3,-3,-3,-3,-3,-3,-3], data, charsize=0.5
END

PRO PLOT_GENERIC, data, parts, spots, nSpots, dataLength, colors1, colors2
  ; plot legend
  plot, [1], xrange=[0,22], yrange=[0,7], ystyle=4, xstyle=4, background='ffffff'x, /nodata
  LEGENDPLOT, parts, colors1, colors2
  plot, [1], xrange=[0,22], yrange=[0,7], ystyle=4, xstyle=4, background='ffffff'x, /nodata
  ; plot data
  yrange= [0, max(data) + 1]
  for i = 0, nSpots - 1 do begin
    spotData = data[i * dataLength:i * dataLength + dataLength - 1]
    plot, spotData, xrange=[0,22], yrange=yrange, title=spots[i], xtickformat='(A1)', ytickformat='(A1)', background='ffffff'x, /nodata
    BARPLOT, spotData, colors1, colors2
  endfor
END

PRO PLOT_DETAIL, data, parts, spots, nSpots, dataLength, colors1, colors2
  ; plot data
  yrange= [0, max(data) + 5]
  for i = 0, nSpots - 1 do begin
    spotData = data[i * dataLength:i * dataLength + dataLength - 1]
    plot, spotData, xrange=[0,22], yrange=yrange, title=spots[i], ytitle='Mikrogramm/cdm', xtickformat='(A1)', background='ffffff'x, /nodata
    BARPLOT, spotData, colors1, colors2
    PLOT_VALUES, spotData
    PLOT_XLABELS, parts
  endfor
END

PRO PLOT_PERIOD, data, parts, spots, nParts, nSpots, dataLength, colors, date1, date2
  periodLength = n_elements(data) / (nSpots * nParts * 2)
  dataLength = dataLength / periodLength
  spotDataLength = n_elements(data) / periodLength
  dataElements = lindgen((n_elements(data) + 1) / 2) * 2
  xrange = [0, periodLength]
  yrange = [0, max(data[dataElements]) + 5]
  xtitle = date1 + " :: " + date2
  
  for i = 0, nSpots - 1 do begin
    for j = 0, nParts - 1 do begin
      partData = findgen(periodLength)
      for k = 0, periodLength - 1 do begin
	x = k * spotDataLength + (j * 2) + (i * dataLength)
	partData[k] = data[x]
      endfor
      if j eq 0 then begin
	plot, partData, xrange=xrange, yrange=yrange, title=spots[i], xtitle=xtitle, xtickformat='(A1)', ytitle='Mikrogramm/cdm', background='ffffff'x, color='000000'x, /nodata
      endif
      oplot, partData, color=colors[j]
    endfor
  endfor
END

PRO BLUME, places, particles, data, mode, date1, date2
  @plotvars
  ; convert places and particles strings to arrays
  spots = strsplit(places, ';', /extract)
  parts = strsplit(particles, ';', /extract)
  ; some variables
  nSpots = n_elements(spots)		; number of places
  nParts = n_elements(parts)		; number of particles
  dataLength = n_elements(data) / nSpots	; the number of data belonging to each spots
  
  set_plot, "ps"
  ; set hardware font to zero to use postsript font
  !P.FONT = 0
  
  case mode of
    1: begin
	device, filename='day_overview.eps', $
	  /encapsulated, $
	  /color, $
	  xsize=21.0, ysize=29.7, $
	  decomposed=1
	  ; set number of plots in file
	!P.MULTI = [0, 2, 9]
	
	plot_generic, data, parts, spots, nSpots, dataLength, barColors1, barColors2
       end
    2: begin
	device, filename='day_detail.ps', $
	  /color, $
	  xsize=21.0, ysize=29.7, $
	  decomposed=1
	  ; set number of plots in file
	!P.MULTI = [0, 1, 2]
	
	plot_detail, data, parts, spots, nSpots, dataLength, barColors1, barColors2
       end
    3: begin
	device, filename='period.ps', $
	  /color, $
	  xsize=21.0, ysize=29.7, $
	  decomposed=1
	  ; set number of plots in file
	!P.MULTI = [0, 1, 2]
	
	plot_period, data, parts, spots, nParts, nSpots, dataLength, barColors1, date1, date2
       end
  else: print, "no mode selected!"
  endcase
  
  device, /close
  set_plot, "x"
  !P.MULTI = 0
  !P.FONT = 1
END